package com.bac.bacplatform;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.integer;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Dialog;
import android.content.Intent;

import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.javabeans.GasStationDetail;
import com.bac.javabeans.Location;
import com.bac.javabeans.Principal;
import com.bac.utils.Config;
import com.bac.utils.GasListAdapter;
import com.bac.utils.Storage;
import com.bac.utils.Util;
import com.baidu.mapapi.model.LatLng;
import com.google.gson.Gson;

public class GasInformationActivity extends BaseActivity implements
		OnClickListener {

	private TextView gasName;// 加油站名
	private TextView gasCount;// 加油站分
	private TextView gasAddress;// 加油站地址
	private ListView gasPrice;
	private TextView gasUpdateTime;// 加油站数据更新时间
	private TextView gasMarkt;// 加油站营销活动
	private RelativeLayout setScore;// 给加油站评分
	private RelativeLayout setRevise;// 信息纠错
	private RelativeLayout btn1;// 刷卡
	private RelativeLayout btn2;// 办理加油卡
	private RelativeLayout btn3;// 圈存机
	private RelativeLayout btn4;// 自助加油
	private RelativeLayout btn5;// 洗车服务
	private RelativeLayout btn6;// 维修服务
	private RelativeLayout btn7;// 超市
	private RelativeLayout btn8;// 开发票
	private RelativeLayout go;// 出发
	private TextView gi_service_ps1_text;// 起始时间
	private TextView gi_service_ps2_text;// 结束时间
	private ImageView star1;
	private ImageView star2;
	private ImageView star3;
	private ImageView star4;
	private ImageView star5;

	public static Location location;// 加油站数据

	private GasListAdapter gla;

	public LatLng st;
	public LatLng en;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gas_information);

		st = new LatLng(Double.valueOf(getIntent().getStringExtra("st_jd")),
				Double.valueOf(getIntent().getStringExtra("st_wd")));
		en = new LatLng(Double.valueOf(getIntent().getStringExtra("end_jd")),
				Double.valueOf(getIntent().getStringExtra("end_wd")));

		findViewById(R.id.title_left).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finishActivityOut();
			}
		});
	}

	@Override
	public void init() {
		location = (Location) getIntent().getSerializableExtra("location");

		star1 = (ImageView) findViewById(R.id.star1_img);
		star2 = (ImageView) findViewById(R.id.star2_img);
		star3 = (ImageView) findViewById(R.id.star3_img);
		star4 = (ImageView) findViewById(R.id.star4_img);
		star5 = (ImageView) findViewById(R.id.star5_img);

		gi_service_ps1_text = (TextView) findViewById(R.id.gi_service_ps1_text);
		gi_service_ps2_text = (TextView) findViewById(R.id.gi_service_ps2_text);
		gasName = (TextView) findViewById(R.id.gi_gas_name_text);
		gasCount = (TextView) findViewById(R.id.gi_score_text);
		gasAddress = (TextView) findViewById(R.id.gi_address_text);
		gasPrice = (ListView) findViewById(R.id.information_gas_type);
		gasUpdateTime = (TextView) findViewById(R.id.gi_pstime_text);
		gasMarkt = (TextView) findViewById(R.id.gi_market_information_text);

		gla = new GasListAdapter(this, location.getDetail().getOil());
		if (location.getDetail().getOil() != null)
			gasPrice.setAdapter(gla);

		gasName.setText(location.getName());

		double startcon = location.getRating() == null ? 0 : location
				.getRating();
		showStar((int) startcon);

		gasCount.setText(location.getRating() == null ? "0" : location
				.getRating() + "");
		gasAddress.setText(location.getAddress());
		if (location.getDetail().getClosingTime() != null) {
			gasUpdateTime.setText(location.getDetail().getClosingTime()
					.toString());
			
			gi_service_ps1_text.setText("加油卡办理时间：");
			gi_service_ps2_text.setText("圈存机服务时间：");
			
			gi_service_ps1_text.setText(gi_service_ps1_text.getText()
					+ location.getDetail().getOpeningTime() + "~"
					+ location.getDetail().getClosingTime());
			gi_service_ps2_text.setText(gi_service_ps2_text.getText()
					+ location.getDetail().getOpeningTime() + "~"
					+ location.getDetail().getClosingTime());
		}

		setScore = (RelativeLayout) findViewById(R.id.gi_score_layout);
		setScore.setOnClickListener(this);
		setRevise = (RelativeLayout) findViewById(R.id.gi_change_errpr_layout);
		setRevise.setOnClickListener(this);

		btn1 = (RelativeLayout) findViewById(R.id.gi_btn1);
		btn2 = (RelativeLayout) findViewById(R.id.gi_btn2);
		btn3 = (RelativeLayout) findViewById(R.id.gi_btn3);
		btn4 = (RelativeLayout) findViewById(R.id.gi_btn4);
		btn5 = (RelativeLayout) findViewById(R.id.gi_btn5);
		btn6 = (RelativeLayout) findViewById(R.id.gi_btn6);
		btn7 = (RelativeLayout) findViewById(R.id.gi_btn7);
		btn8 = (RelativeLayout) findViewById(R.id.gi_btn8);
		buttonGone();
		go = (RelativeLayout) findViewById(R.id.gi_go_layout);
		go.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.gi_score_layout:
			showScoreDialog();
			break;
		case R.id.gi_change_errpr_layout:
			showErrorDialog();
			break;
		case R.id.gi_go_layout:
			Intent intent = new Intent();
			intent.setClass(GasInformationActivity.this, GoToGasActivity.class);
			intent.putExtra("end_jd", getIntent().getStringExtra("end_jd"));
			intent.putExtra("end_wd", getIntent().getStringExtra("end_wd"));
			intent.putExtra("st_jd", getIntent().getStringExtra("st_jd"));
			intent.putExtra("st_wd", getIntent().getStringExtra("st_wd"));
			startActivityIn(intent);
			break;

		default:
			break;
		}
	}

	public void showStar(int score) {
		switch (score) {
		case 1:
			star1.setVisibility(View.VISIBLE);
			break;
		case 2:
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			break;
		case 3:
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			star3.setVisibility(View.VISIBLE);
			break;
		case 4:
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			star3.setVisibility(View.VISIBLE);
			star4.setVisibility(View.VISIBLE);
			break;
		case 5:
			star1.setVisibility(View.VISIBLE);
			star2.setVisibility(View.VISIBLE);
			star3.setVisibility(View.VISIBLE);
			star4.setVisibility(View.VISIBLE);
			star5.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
	}

	@SuppressWarnings("deprecation")
	private void showErrorDialog() {
		Dialog dialog = new MyDialog(GasInformationActivity.this,
				R.style.MyDialog);
		Window window = dialog.getWindow();
		window.setGravity(Gravity.BOTTOM);
		window.setWindowAnimations(R.style.mystyle);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		// 设置对话框大小
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
		lp.width = (int) (display.getWidth()); // 设置宽度
		dialog.getWindow().setAttributes(lp);

	}

	@SuppressWarnings("deprecation")
	private void showScoreDialog() {
		Dialog dialog = new ScoreDialog(GasInformationActivity.this,
				R.style.ScoreDialog);
		Window window = dialog.getWindow();
		window.setGravity(Gravity.BOTTOM);
		window.setWindowAnimations(R.style.mystyle);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		// 设置对话框大小
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
		lp.width = (int) (display.getWidth()); // 设置宽度
		lp.height = (int) (display.getHeight() * 0.4);
		dialog.getWindow().setAttributes(lp);
	}

	private void buttonGone() {
		if (!location.getDetail().isAcceptingUnionPayCard()) {
			btn1.setVisibility(View.GONE);
		}
		if (!location.getDetail().isCardIssuing()) {
			btn2.setVisibility(View.GONE);
		}
		if (!location.getDetail().isCardLoading()) {
			btn3.setVisibility(View.GONE);
		}
		if (!location.getDetail().isCarWashingService()) {
			btn5.setVisibility(View.GONE);
		}
		if (!location.getDetail().isAutoRepairService()) {
			btn6.setVisibility(View.GONE);
		}
		if (!location.getDetail().isShoppingService()) {
			btn7.setVisibility(View.GONE);
		}
		if (!location.getDetail().isInvoicing()) {
			btn8.setVisibility(View.GONE);
		}
	}
}
